class Admin::RestaurantsController < ApplicationController
  filter_access_to :all

  def new
    # questo metodo deve mostrare un form per riempire i campi del ristorante
    @restaurant = Restaurant.new
    @restaurant.account = Account.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @restaurant }
    end
  end

  def create
    @restaurant = Restaurant.new(params[:restaurant])
    @restaurant.account = Account.new(params[:account])

    respond_to do |format|
      if @restaurant.save
        flash[:notice] = 'La pizzeria è stata salvata con successo!'
        format.html { redirect_to root_path }
        format.xml  { render :xml => @restaurant, :status => :created, :location => @restaurant }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @restaurant.errors, :status => :unprocessable_entity }
      end
    end
  end

  def index
    @restaurants = Restaurant.find(:all, :order => :name)
  end

  def edit
    @restaurant = Restaurant.find(params[:id])
  end

  def update
    @restaurant = Restaurant.find(params[:id])
    respond_to do |format|
      if @restaurant.update_attributes(params[:restaurant]) and
          @restaurant.account.update_attributes(params[:account])
        flash[:notice] = 'La pizzeria è stata modificata'
        format.html { redirect_to admin_restaurants_path }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @restaurant.errors, :status => :unprocessable_entity }
      end
    end
  end

  def destroy
    if Restaurant.find(params[:id]).destroy
      flash[:notice] = "La pizzeria è stata eliminata!"
    else
      flash[:error] = "Errore di sistema"
    end
    redirect_to admin_restaurants_path
  end
end
